/* Copyright 2011 Elektrobit Wireless Communications Oy (EB)


  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License version 2 as 
  published by the Free Software Foundation.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License along
  with this program; if not, write to the Free Software Foundation, Inc.,
  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.


 *All rights reserved 
 *Version:Elektrobit_Qt_1.0.0_20110305 */


#ifndef PLAYER_SERVICE_H
#define PLAYER_SERVICE_H
#ifdef C2PLAYER
#include "service.h"
#include "msp/PlaybackWidgetInterface.h"
#include <stdlib.h>
#include <unistd.h>
#include <asm/c2fb.h>
#include <asm/c2osd.h>
#include <asm/c2videodev2.h>
#include <linux/videodev2.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <QTimer>

#define WHITE   ((180 << 22) | (128 << 12) | (128<<2))
#define YELLOW  ((162 << 22) | ( 44 << 12) | (142<<2))
#define CYAN    ((131 << 22) | (156 << 12) | ( 44<<2))
#define GREEN   ((112 << 22) | ( 72 << 12) | ( 58<<2))
#define MAGENTA (( 84 << 22) | (184 << 12) | (198<<2))
#define RED     (( 65 << 22) | (100 << 12) | (212<<2))
#define BLUE    (( 35 << 22) | (212 << 12) | (114<<2))
#define BLACK   (( 16 << 22) | (128 << 12) | (128<<2))

void PlaybackCallback(MspWidget* pWidget, InfoType cmdId, void* data);

//----------------------------
//分辨率
typedef struct _st_resolve_t_{
    int width;
    int height;
}st_resolve_t;

//----------------------------
//播放窗口
typedef struct _st_window_t_{
    int x;
    int y;
    int w;
    int h;
}st_window_t;

//----------------------------
//播放器状态

typedef enum{
    PlayerStop=1,PlayerPause,PlayerRunning,PlayerScan
}PlayerStatus;


class PlayerService : public Service
{
    Q_OBJECT
    Q_PROPERTY( MODE mode READ getMode WRITE setMode )
    Q_ENUMS(MODE)
public:

    enum MODE { VGA,HDMI,CVBS,YUV };
    MODE getMode(){ return mMode; }
    void setMode(MODE m){ this->mMode = m; }
    PlayerService(QObject *parent = 0);
    int start();

    int initc2();

    //------------------------------------------------
    //播放一个视频文件，参数path是视频文件的路径
    //返回值表示播放是否成功
    //此函数是非堵塞函数，调用以后立即返回
    int play(const QString & path);

    //------------------------------------------------
    //播放一个视频文件，参数path是视频文件的路径,seektime
    //是播放文件得起始位置,单位毫秒, starttime是视频文件播放
    //的开始时间
    //返回值表示播放是否成功
    //此函数是非堵塞函数，调用以后立即返回
    int playseekfile(qint64 startime,const QString & path, qint64 seektime);

    //------------------------------------------------
    //停止播放视频文件
    //此函数是非堵塞函数，调用以后立即返回
    int stop();

    //------------------------------------------------
    //暂停播放视频文件
    //不论在正常播放，快进，快退都能暂停
    int pause();

    //------------------------------------------------
    //恢复播放视频文件
    //恢复到播放 状态
    int resume();

    //------------------------------------------------
    //快进
    //fast: true为快进， false为慢进
    int forward(bool fast);

    //------------------------------------------------
    //快退
    //slow: true为快退，  false为慢退
    int backward(bool slow);

    //------------------------------------------------
    //把播放文件定位到新位置
    //step为重定位的步长， 正数为像前定位，负数为向后定位,单位为毫秒
    int seek(u64 step);

    //------------------------------------------------
    //设置音量，范围 0-100
    int setVolume(int vol);

    //------------------------------------------------
    //得到当前音量
    //返回值为当前音量大小， 范围为0－100
    int getVolume();

    //------------------------------------------------
    //得到当前播放状态
    //返回值包括：播放状态，暂停状态，快进状态
    //此函数是非堵塞函数，调用以后立即返回
    PlayerStatus getStatus();

    //------------------------------------------------
    //得到当前播放时长
    //返回值为当前播放时长，单位精度微秒？
    int getPosition(u64* pPos);

    //------------------------------------------------
    //得到视频文件总时长
    //返回值为当前文件总时长，单位精度微秒？
    int getTotal(u64* pTot);

    void playerFinished();

    void setPlayBackWidget(PlaybackWidget *widget) { pPlaybackWidget = widget; }
signals:
    void playFinished(const QString & key);
protected slots:
    void onTimer();
protected:
    void timerEvent(QTimerEvent *event);
private:
    PlaybackWidget *pPlaybackWidget;
    MspWidgetConfiguration config;
    MODE mMode;
    QString mPath;
    qint64 mStartTime;
    qint64 mSeekTime;
    QTimer * mTimer;
    int timerID;
    bool isChecked;
    int CheckTimeCount;
};
Q_DECLARE_METATYPE(PlayerService);
#endif
#endif // PLAYER_SERVICE_H
